package com.yinhai.ta404.module.hunan.userarea.service.write.impl;

import com.yinhai.ta404.component.org.core.service.impl.OrgBaseService;
import com.yinhai.ta404.core.exception.AppException;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.core.utils.ValidateUtil;
import com.yinhai.ta404.module.hunan.userarea.entity.TaUserAreaPo;
import com.yinhai.ta404.module.hunan.userarea.mapper.write.UserAreaWriteMapper;
import com.yinhai.ta404.module.hunan.userarea.service.read.UserAreaReadService;
import com.yinhai.ta404.module.hunan.userarea.service.write.UserAreaWriteService;
import com.yinhai.ta404.module.mybatis.batch.IBatchService;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service
@TaTransactional
public class UserAreaWriteServiceImpl extends BaseService implements UserAreaWriteService {
    @Resource
    private UserAreaReadService userAreaReadService;
    @Resource
    private UserAreaWriteMapper userAreaWriteMapper;
    @Resource
    private OrgBaseService orgBaseService;
    @Autowired
    protected IBatchService batchService;

    @Override
    public void updateUserArea(String userId, List<String> addAreaIds, List<String> removeAreaIds, String curUserId) {
        if (ValidateUtil.isEmpty(userId)) {
            throw new AppException("用户编号为空，无法保存人员行政区划！");
        }

        this.batchService.batchOperation(UserAreaWriteMapper.class, "deleteUserArea", new Object[]{removeAreaIds.stream().map((areaId) -> {
            return new TaUserAreaPo(userId, areaId);
        }).collect(Collectors.toList())}, (SqlSessionTemplate)null);

        this.batchService.batchOperation(UserAreaWriteMapper.class, "addUserArea", new Object[]{addAreaIds.stream().map((areaId) -> {
            return new TaUserAreaPo(userId, areaId);
        }).collect(Collectors.toList())}, (SqlSessionTemplate)null);
    }
}
